package xyz.itpath.model;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/**
 * 结果返回类
 *
 * @author zhaomengru
 * create by  2024/1/2 - 11:19
 */
@Data
@Accessors(chain = true)
public class Result<T> {

    public static Map<Integer, String> CODE_MESSAGE_MAP = new HashMap<>();

    private Integer code;

    private String message;

    private T data;

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("success");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data, String message) {
        return success(data).setMessage(message);
    }

    public static <T> Result<T> error(Integer code) {
        return new Result<T>()
                .setCode(200)
                .setData(null)
                .setMessage(CODE_MESSAGE_MAP.getOrDefault(code, "服务器内部错误"));
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<T>()
                .setCode(code)
                .setData(null)
                .setMessage(message);
    }

    public static <T> Result<T> error500(String message) {
        return Result.error(500, message);
    }
}
